package com.zwj.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName IntOptionsValid
 * @Description TODO
 * Author zwJ
 * Date 2020/8/16 18:10
 * Version 1.0
 **/
public class IntOptionsValid implements ConstraintValidator<IntOptions, Integer> {

    private Set<Integer> optionSet = new HashSet<>();

    /**
     * 初始化
     *
     * @param constraintAnnotation
     */
    @Override
    public void initialize(IntOptions constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if (vals != null && vals.length > 0) {
            for (int i = 0; i < vals.length; i++) {
                optionSet.add(vals[i]);
            }
        }

    }

    /**
     * 校验
     *
     * @param integer
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        if (optionSet.contains(integer)) {
            return true;
        }
        return false;
    }
}
